//===================================================================================
// Microsoft patterns & practices
// Composite Application Guidance for Windows Presentation Foundation and Silverlight
//===================================================================================
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===================================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===================================================================================
using System;
using System.Globalization;
using System.Windows;
using ViewSwitchingNavigation.Properties;

namespace ViewSwitchingNavigation
{
    public partial class App : Application
    {
        private const char DoubleQuote = '"';
        private const char EscapedSingleQuoteCharacter = '\'';
        private const string EscapedNewLine = "\r\n";
        private const string PreferredNewLine = @"\n";

        public App()
        {
            this.Startup += Application_Startup;
            this.Exit += Application_Exit;
            this.UnhandledException += this.Application_UnhandledException;

            InitializeComponent();
        }

        private static void Application_Startup(object sender, StartupEventArgs e)
        {
            QuickStartBootstrapper bootstrapper = new QuickStartBootstrapper();
            bootstrapper.Run(true);
        }

        private static void Application_Exit(object sender, EventArgs e)
        {

        }

        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            // If the app is running outside of the debugger then report the exception using
            // the browser's exception mechanism. On IE this will display it a yellow alert 
            // icon in the status bar and Firefox will display a script error.
            if (!System.Diagnostics.Debugger.IsAttached)
            {

                // NOTE: This will allow the application to continue running after an exception has been thrown
                // but not handled. 
                // For production applications this error handling should be replaced with something that will 
                // report the error to the website and stop the application.
                e.Handled = true;
                Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
            }
        }

        private static void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
        {
            try
            {
                string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
                errorMsg = errorMsg.Replace(DoubleQuote, EscapedSingleQuoteCharacter).Replace(EscapedNewLine, PreferredNewLine);
                string errorCommand = string.Format(CultureInfo.CurrentCulture,
                                                    StringResources.JavascriptThrowNewErrorCommandFormatString, errorMsg);
                System.Windows.Browser.HtmlPage.Window.Eval(errorCommand);
            }
            catch (Exception)
            {
            }
        }
    }
}
